<?php

class Core_Backend_Component_Media_Controller_Video extends Dcmp_Component_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function indexAction($pageindex = 1, $pagesize = 20, $sort = 'id', $dir = 'asc', $areaid = 0) {
        $this->getHistory()->record();
        $validator = new Kd_Validation_Validator();
        $validator->setTarget($this->getRequest()->getParameter('filter'));
        $videos = App::getModel('media/collection_video')
            ->joinAccount()
            ->pagination($pageindex, $pagesize);
        if(!$this->getIdentity()->isInRole('video_reviewer')){
            $videos->filterByCreatorId($this->getIdentity()->getId());
        }
        if ($validator->isNotEmpty('name')) {
            $videos->filterByName($validator->getValue('name'));
        }
        $videos->load();
        $records = $videos->loadCount();
        return $this->viewResult(array('list' => $videos, 'records' => $records));
    }

    public function createAction() {
        return $this->viewResult();
    }

    public function editAction($int_id) {
        $model = App::getModel('media/video')->load($int_id);
        return $this->createAction($model->getAreaId())->setModel($model);
    }

    public function savePostAction($status = 'draft') {
        $data = $this->getRequest()->getForm('video');
        $model = App::getModel('media/video');
        if (isset($data['id']) && !empty($data['id'])) {
            $model->load($data['id']);
        }
        $model->addData($data)->setStatus($status);
        $savedModel = $model;
        if (($errors = $model->validate() === TRUE)) {
            $model->save();
            $this->getMessager()->addInform($this->__('share.message.success'));
            return $this->redirectResult($this->getHistoryUrl('media/video/index'));
        } else {
            $this->getMessager()->addError($this->__('share.message.fail'))
                    ->addError($errors);
            return $this->createAction()->setModel($model);
        }
    }
    
    public function reviewPostAction($ids, $status, $statusmessage = ''){
    	if($status == 'published'){
        	App::getModel('media/video')->publish($ids);
    	}elseif($status == 'forbidden'){
    		App::getModel('media/video')->forbid($ids, $statusmessage);
    	}
        return $this->redirectResult($this->getHistoryUrl('media/video/index'));    	
    }
}